package com.uetoken.cn.activity;

import android.os.Build;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.uetoken.cn.R;
import com.uetoken.cn.adapter.PageNumBottonAdapter;
import com.uetoken.cn.adapter.PurchaseDetailAdapter;
import com.uetoken.cn.adapter.model.PageNumBottonModel;
import com.uetoken.cn.adapter.model.PurchaseDetailTableModel;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.manager.UEDataManager;
import com.uetoken.cn.view.pagerecyclerview.PagingScrollHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PurchaseDetailActivity extends BaseActivity {


    @BindView(R.id.stateTv)
    TextView stateTv;
    @BindView(R.id.currencyTv)
    TextView currencyTv;
    @BindView(R.id.quantityBtn)
    TextView quantityBtn;
    @BindView(R.id.typeBtn)
    TextView typeBtn;
    @BindView(R.id.timeBtn)
    TextView timeBtn;
    @BindView(R.id.tableRecyclerView)
    RecyclerView mTableRecyclerView;
    @BindView(R.id.tableLayout)
    LinearLayout tableLayout;
    @BindView(R.id.nextPageBtn)
    Button mNextPageBtn;
    @BindView(R.id.pageNumRecyclerView)
    RecyclerView mPageNumRecyclerView;
    @BindView(R.id.previousPageBtn)
    Button mPreviousPageBtn;
    @BindView(R.id.footPageLayout)
    RelativeLayout mFootPageLayout;
    @BindView(R.id.topbar)
    QMUITopBar mTopbar;


    private PurchaseDetailAdapter mPurchaseDetailAdapter;
    private PageNumBottonAdapter mPageNumBottonAdapter;
    private DividerItemDecoration vDividerItemDecoration = null;
    private LinearLayoutManager vLinearLayoutManager = null;
    private PagingScrollHelper mScrollHelper;

    private List<PurchaseDetailTableModel> mPurchaseDetailTableList = new ArrayList<>();
    private List<PageNumBottonModel> pageSumList = new ArrayList<>();
    private int pageCurrent = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_purchase_detail;
    }

    @Override
    public void initView() {
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        }
        ButterKnife.bind(this);
        initTopBar();
        initTableLayout();
        initBottomPagerView();
    }

    @Override
    public void initData() {
        getTableData();

        mPurchaseDetailAdapter = new PurchaseDetailAdapter(this);
        mTableRecyclerView.setAdapter(mPurchaseDetailAdapter);
        mScrollHelper.setUpRecycleView(mTableRecyclerView);

        getPageNumData();
        mPageNumBottonAdapter = new PageNumBottonAdapter(this, null);
        mPageNumRecyclerView.setAdapter(mPageNumBottonAdapter);

        mPageNumBottonAdapter.setOnItemClickListener(new PageNumBottonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                pageCurrent = postion;
                mScrollHelper.setPageNum(pageCurrent + 1);
                ToastUtils.showShort("mPageNumBottonAdapter = " + postion);
                LogUtils.dTag("mPageNumBottonAdapter==", "mPageNumBottonAdapter = " + postion);
                setPageCurrentView(pageCurrent);
            }
        });
//        mPageNumBottonAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                pageCurrent = position;
//                ToastUtils.showShort("mPageNumBottonAdapter = " + position);
//                LogUtils.dTag("mPageNumBottonAdapter==", "mPageNumBottonAdapter = " + position);
//                setPageCurrentView(pageCurrent);
//            }
//        });
    }

    @Override
    public void init() {

    }

    private void initTopBar() {
        mTopbar.setTitle(R.string.purchase_details_title);
        mTopbar.addLeftImageButton(R.drawable.ic_back, R.id.qmui_topbar_item_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void getTableData() {
        for (int i = 1; i <= 100; i++) {
            PurchaseDetailTableModel mPurchaseDetailTableModel = new PurchaseDetailTableModel();
            switch (i % 3) {
                case 0:
                    mPurchaseDetailTableModel.setStateDra(R.mipmap.icon_succeed_right);
                    mPurchaseDetailTableModel.setCurrencyUnit("BTC");
                    mPurchaseDetailTableModel.setQuantity("4.85654846");
                    mPurchaseDetailTableModel.setType(i + "");
                    mPurchaseDetailTableModel.setTime("2018-06-12 12:45");
                    break;
                case 1:
                    mPurchaseDetailTableModel.setStateDra(R.mipmap.icon_failure_wrong);
                    mPurchaseDetailTableModel.setCurrencyUnit("BTH");
                    mPurchaseDetailTableModel.setQuantity("5.85654847");
                    mPurchaseDetailTableModel.setType(i + "");
                    mPurchaseDetailTableModel.setTime("2018-06-13 12:46");
                    break;
                case 2:
                    mPurchaseDetailTableModel.setStateDra(R.mipmap.icon_failure_wrong);
                    mPurchaseDetailTableModel.setCurrencyUnit("BTH");
                    mPurchaseDetailTableModel.setQuantity("6.85654848");
                    mPurchaseDetailTableModel.setType(i + "");
                    mPurchaseDetailTableModel.setTime("2018-06-14 12:47");
                    break;
            }
            mPurchaseDetailTableList.add(mPurchaseDetailTableModel);
        }
        UEDataManager.getInstance().setPurchaseDetailTableList(mPurchaseDetailTableList);
    }

    private void initTableLayout() {
        mScrollHelper = new PagingScrollHelper();

        vDividerItemDecoration = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
        vLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

        //设置布局管理器 , 将布局设置成纵向
//        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this);
        //设置分隔线
//        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
//        mTableRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        mTableRecyclerView.setLayoutManager(vLinearLayoutManager);
        mTableRecyclerView.addItemDecoration(vDividerItemDecoration);
        // 添加Android自带的分割线
    }

    private void getPageNumData() {
        double pageCount_double = mPurchaseDetailTableList.size() / 10;
        int pageCount_int = (int) pageCount_double;
        if (pageCount_double > pageCount_int) {
            pageCount_int = pageCount_int + 1;
        }
        LogUtils.dTag("initBottomPagerView==", "initBottomPagerView== getPageCount = " + pageCount_int);
        for (int i = 1; i <= pageCount_int; i++) {
            PageNumBottonModel mPageNumBottonModel = new PageNumBottonModel();
            mPageNumBottonModel.setPageNum(i);
            boolean isPage = false;
            if (i == 1) {
                isPage = true;
            } else {
                isPage = false;
            }
            mPageNumBottonModel.setIsPage(isPage);
            pageSumList.add(mPageNumBottonModel);
        }
        UEDataManager.getInstance().setPageSumList(pageSumList);
    }

    private void initBottomPagerView() {
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mPageNumRecyclerView.setLayoutManager(mLinearLayoutManager);
//        mPageNumRecyclerView.addItemDecoration();
    }

    private void setPageCurrentView(int pageCurrent) {

        for (int i = 0; i < pageSumList.size(); i++) {
            PageNumBottonModel mPageNumBottonModel = pageSumList.get(i);
            boolean isSelected = false;
            if (i == pageCurrent) {
                isSelected = true;
            } else {
                isSelected = false;
            }
            mPageNumBottonModel.setIsPage(isSelected);
        }
        UEDataManager.getInstance().setPageSumList(pageSumList);
        mPageNumBottonAdapter.notifyDataSetChanged();
    }
}
